Dynamic Objects in Golo

Getting Started with Golo

A Dynamic Object lets you add properties and methods on the fly, without defining a structure in advance.


Creating and Setting Properties

Use : propertyName(value) to set, : propertyName() to read.

let person = DynamicObject()
  : name("Alice")
  : age(30)

println(person: name())  # Alice
println(person: age())   # 30

Adding Methods

Methods are lambdas where the first parameter is always this.

let person = DynamicObject()
  : name("Alice")
  : greet(|this| {
      println("Hello, I'm", this: name())
  })

person: greet()  # Hello, I'm Alice

Methods can take additional parameters:

let calc = DynamicObject()
  : add(|this, a, b| { return a + b })

println(calc: add(5, 3))  # 8

Method Chaining

Return this from methods to enable chaining.

let counter = DynamicObject()
  : value(0)
  : increment(|this| {
      this: value(this: value() + 1)
      return this
  })

counter: increment(): increment(): increment()
println(counter: value())  # 3

Introspection

let obj = DynamicObject()
  : id(1)
  : name("test")
  : save(|this| { println("Saving...") })

println(obj: properties())  # [id, name]
println(obj: methods())     # [save]

JSON Serialization

Only properties are serialized — methods are ignored.

let person = DynamicObject()
  : name("Alice")
  : age(30)
  : greet(|this| { println("Hello") })

println(toJSON(person))  # {"name":"Alice","age":30}

© 2026 GoloScript Project | Built with Gu10berg

Subscribe: 📡 RSS | ⚛️ Atom